package com.dongdongshop.service.impl;

import com.dongdongshop.admin.model.TbItemCat;
import com.dongdongshop.admin.model.TbItemCatExample;
import com.dongdongshop.admin.vo.ItemCatVO;
import com.dongdongshop.mapper.TbItemCatMapper;
import com.dongdongshop.service.ItemCatService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ItemCatServiceImpl implements ItemCatService {
    @Autowired
    private TbItemCatMapper itemCatMapper;
    @Override
    public List<ItemCatVO> getByParentId(Long parentId) {
        TbItemCatExample e = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = e.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<TbItemCat> tbItemCats = itemCatMapper.selectByExample(e);
        List<ItemCatVO> collect = tbItemCats.stream().map(tbItemCat -> {
            ItemCatVO catVO = new ItemCatVO();
            BeanUtils.copyProperties(tbItemCat, catVO);
            return catVO;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public void add(ItemCatVO catVO) {
        TbItemCat cat = new TbItemCat();
        BeanUtils.copyProperties(catVO, cat);
        itemCatMapper.insertSelective(cat);
    }

    @Override
    public ItemCatVO getById(Long id) {
        TbItemCat cat = itemCatMapper.selectByPrimaryKey(id);
        ItemCatVO cat1 = new ItemCatVO();
        BeanUtils.copyProperties(cat, cat1);
        return cat1;
    }

    @Override
    public void update(ItemCatVO catVO) {
        TbItemCat cat = new TbItemCat();
        BeanUtils.copyProperties(catVO, cat);
        itemCatMapper.updateByPrimaryKeySelective(cat);
    }

    @Override
    public void delete(Long[] ids) {
        try {
            TbItemCatExample e = new TbItemCatExample();
            TbItemCatExample.Criteria criteria = e.createCriteria();
            criteria.andIdIn(Arrays.asList(ids));
            //删除子表
            for (int i = 0; i < ids.length; i++) {
                TbItemCat cat = itemCatMapper.selectByPrimaryKey(ids[i]);
                TbItemCatExample e1 = new TbItemCatExample();
                TbItemCatExample.Criteria criteria1 = e1.createCriteria();
                criteria1.andParentIdEqualTo(cat.getId());
                itemCatMapper.deleteByExample(e1);
            }
            //删除主表
            itemCatMapper.deleteByExample(e);
        }catch (Exception e) {
            throw e;
        }

    }

    //    查询模板id
    @Override
    public Long selectId(Long id) {
        if (id != 0) {
            //获取tyid
            TbItemCat tbItemCat = itemCatMapper.selectTypeId(id);
            //通过typeid 获取模板id
            return tbItemCat.getTypeId();
        }
        return 0L;
    }

    @Override
    public List<ItemCatVO> getAll() {
        List<TbItemCat> tbItemCats = itemCatMapper.selectByExample(null);
        List<ItemCatVO> collect = tbItemCats.stream().map(i -> {
            ItemCatVO vo = new ItemCatVO();
            BeanUtils.copyProperties(i, vo);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public ItemCatVO getId(Long parentId) {
        TbItemCat cat = itemCatMapper.selectByPrimaryKey(parentId);
        ItemCatVO vo = new ItemCatVO();
        BeanUtils.copyProperties(cat, vo);
        return vo;
    }
}
